<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('action'=>'jy_xyb2b/goods/list/status=0','title'=>'未上线商品'),
	array('action'=>'jy_xyb2b/goods/list/status=1','title'=>'已上线商品'),
	array('action'=>'jy_xyb2b/goods/list/status=2','title'=>'已剔除商品'),
);
$return['status']=$this->goods_status;
//上架商品
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','star','status','total','price','oldprice','appid','category_id1','category_id2','category_id3','title','goods_img','paixu');
	$list=pdo_getslice('jy_xyb2b_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc, price desc, id desc');
	if($list){
		foreach($list as &$item){
				$item['title']=cfc('tools')->substr($item['title'],0,22);
				$item['category_id1title']=pdo_getcolumn('jy_xyb2b_group',array('category_id'=>$item['category_id1']),'title');
				$item['category_id1title']=$item['category_id1title']?$item['category_id1title']:'无';
				$item['url']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：这里显示的所有仓库商品，上线了的商品，其他商户才能看到，才可以被选择','list'=>array(
			array('name'=>'paixu','title'=>'排序_The sorting','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称_Goods title','img'=>'goods_img',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('name'=>'category_id1title','title'=>'分类','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
			array('name'=>'status','title'=>'入B2B库？','type'=>'switch','set'=>'set'),
		),'action'=>array(
		
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_xyb2b/goods/checked1/','title'=>'一键入B2B库'),
			array('type'=>'post','url'=>'jy_xyb2b/goods/checked0/','title'=>'一键下线商品'),
			array('type'=>'post','url'=>'jy_xyb2b/goods/upprice/','title'=>'一键同步成本价'),
			array('type'=>'post','url'=>'jy_xyb2b/goods/uptotal/','title'=>'一键同步库存'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'商品名称_Goods'),
		)
	);
	$return['htmls']=$htmls;
	$return['merchs']=$merchs;
	exi($return);
}
if($_RQ['op']=='upprice'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$goods=pdo_get('jy_xyb2b_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$goods_detail=$this->model('goods')->upgoodsitem($goods);
			$this->model('goods')->upsku_price($goods_detail);
		}
	}
	exi('同步价格成功！');
}
if($_RQ['op']=='uptotal'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$goods=pdo_get('jy_xyb2b_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$goods['sku_list']=json_decode($goods['sku_list'],true);
			if(!$goods['sku_list'] || count($goods['sku_list'])<1){
				exi('请先同步价格！再同步库存！','error');
			}
			$this->model('goods')->upsku_total($goods);
		}
	}
	exi('同步库存成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_xyb2b_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('status'=>1,'total'=>188,'api_id'=>$api_id);
	}
	$return['item']=$item;
	$return['item']['sku_list']=json_decode($return['item']['sku_list'],true);
	$return['item']['goods_thumb_image']=json_decode($return['item']['goods_thumb_image'],true);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>$tabs,
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'paixu','title'=>'排序','type'=>'string','help'=>'数字越大，排名越靠前'),
					array('name'=>'title','title'=>'商品名称','type'=>'string'),
					array('name'=>'goods_img','title'=>'商品图片','type'=>'image','help'=>'建议尺寸：宽580px*高650rpx'),
					array('name'=>'price','title'=>'商品价格','type'=>'string','help'=>'加价后的！'),
					array('name'=>'oldprice','title'=>'原价','type'=>'copy'),
					array('name'=>'goods_thumb_image','title'=>'商品图片','type'=>'images','help'=>'建议尺寸：宽580px*高650rpx'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('必须设置商品名称','error');
	}
	if(!$postdata['goods_img']){
		exi('必须设置商品图片','error');
	}
	if(!$postdata['price']){
		exi('必须设置商品价格','error');
	}
	if(!$postdata['content']){
		exi('必须设置商品详情','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'goods_img'=>trim($postdata['goods_img']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'oldprice'=>floatval($postdata['oldprice']),
		'price'=>floatval($postdata['price']),
		'goods_thumb_image'=>ijson_encode($postdata['goods_thumb_image']),
	);
	$goodsid=intval($postdata['id']);
	if($goodsid){
		pdo_update('jy_xyb2b_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_xyb2b_goods',$data);
		$goodsid=pdo_insertid();
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=$post['status']?0:1;
	}
	if($update){
		pdo_update('jy_xyb2b_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='checked0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_xyb2b_goods',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('下线成功！');
}
if($_RQ['op']=='checked1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_xyb2b_goods',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$goods=pdo_get('jy_xyb2b_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			model('jy_b2b/goods')->upb2bgoods($goods,'jy_xyb2b');
		}
	}
	
	exi('入B2B库成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(count($del)==0){
		exi('请先选择商品','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_xyb2b_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}